package com.example.myapplication.activity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.IMyService;
import com.example.myapplication.R;
import com.example.myapplication.service.MyService;

public class ServiceActivity extends AppCompatActivity {
    final String TAG="MyServiceActivity";
    private Button startButton;
    private Button endButton;
    private EditText mEdit;
    private IMyService iMyService;
    Boolean mBound=false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service);

        Init();
        BindService();
    }
    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            iMyService = IMyService.Stub.asInterface(iBinder);
            mBound=true;
            // 使用aidl跨进程通信的方法
            try {
                iMyService.Log();
            } catch (RemoteException e) {
                throw new RuntimeException(e);
            }
        }

        /**
         * bind的服务意外断开才回调这个接口
         * @param componentName
         */
        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            iMyService=null;
            mBound=false;
            Log.d(TAG, "onServiceDisconnected: 服务连接意外关闭！");
        }
    };
    void Init(){
        mEdit= findViewById(R.id.ser_Edit);
        startButton=findViewById(R.id.ser_start);
        endButton=findViewById(R.id.ser_end);

        startButton.setOnClickListener(this::showServiceMsg);
        endButton.setOnClickListener(this::UnbindService);
    }

    /**
     * 绑定服务
     */
    void BindService(){
        Intent intent=new Intent(this, MyService.class);
        bindService(intent,serviceConnection, Context.BIND_AUTO_CREATE);
    }
    void showServiceMsg(View view){
        if(!mBound) {
            BindService();
        }
//        if(!mBound) return;
        try{
            String msg = iMyService.ShowMsg(mEdit.getText().toString());
            Toast.makeText(ServiceActivity.this, msg, Toast.LENGTH_LONG).show();
        }catch (Exception e){
            throw new RuntimeException(e);
        }

    }

    /**
     * button解绑服务
     * @param view
     */
    void UnbindService(View view){
        if(mBound){
            mBound=false;
            unbindService(serviceConnection);
            Log.d(TAG, "UnbindService----》绑定的服务正在解绑");
        }
    }
    @Override
    protected void onDestroy(){
        super.onDestroy();
        if(mBound){
            iMyService=null;
            unbindService(serviceConnection);
            mBound=false;
        }
    }
}